package com.jt.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import com.google.common.collect.Maps;


public class LoadPropertiesUtil {
	private static Logger log = LoggerFactory.getLogger(LoadPropertiesUtil.class);
	private static Map<String,Properties> map = Maps.newConcurrentMap();
	private static LoadPropertiesUtil loadProperties = new LoadPropertiesUtil();
	
	private LoadPropertiesUtil() {
		
	}
	
	public static LoadPropertiesUtil getInstence(){
		return loadProperties;
	}
	
	public static String getProperties(String propertiesName,String key){
		Properties p = null;
		if(!map.containsKey(propertiesName)){
			p = new Properties();
			File file = null;
			try {
				file = getFile(propertiesName);
				FileInputStream in = new FileInputStream(file);
				p.load(in);
			} catch (Exception e) {
				log.error("classpath:"+propertiesName+"文件未找到", e);
				e.printStackTrace();
			}
			map.put(propertiesName, p);
		}
		
		return map.get(propertiesName).getProperty(key);
	}

	/**
	 * 拿到文件File
	 * @param path
	 * @return
	 * @throws FileNotFoundException
	 */
	public static File getFile(String path) {
		try {
			return ResourceUtils.getFile("classpath:" + path);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	public static void main(String[] args) {
		
	}
	
}
